<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>pdfgauss.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>y&nbsp;</span>=&nbsp;<span class=defun_name>pdfgauss</span>(<span class=defun_in>X,&nbsp;arg1,&nbsp;arg2&nbsp;</span>)<br>
<span class=h1>%&nbsp;PDFGAUSS&nbsp;Evaluates&nbsp;multivariate&nbsp;Gaussian&nbsp;distribution.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;=&nbsp;pdfgauss(X,&nbsp;Mean,&nbsp;Cov)</span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;=&nbsp;pdfgauss(X,&nbsp;model&nbsp;)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;=&nbsp;pdfgauss(X,&nbsp;Mean,&nbsp;Cov)&nbsp;evaluates&nbsp;a&nbsp;multi-variate&nbsp;Gaussian&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;probability&nbsp;density&nbsp;function(s)&nbsp;for&nbsp;given&nbsp;input&nbsp;column&nbsp;vectors&nbsp;in&nbsp;X.</span><br>
<span class=help>%&nbsp;&nbsp;Mean&nbsp;[dim&nbsp;x&nbsp;ncomp]&nbsp;and&nbsp;Cov&nbsp;[dim&nbsp;x&nbsp;dim&nbsp;x&nbsp;ncomp]&nbsp;describe&nbsp;a&nbsp;set&nbsp;of&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;ncomp&nbsp;Gaussian&nbsp;distributions&nbsp;to&nbsp;be&nbsp;evaluted&nbsp;such&nbsp;that</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;y(i,j)&nbsp;=&nbsp;exp(-0.5(mahalan(X(:,j),Mean(:,i),Cov(:,:,i)&nbsp;)))/norm_const</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;where&nbsp;i=1:ncomp&nbsp;and&nbsp;j=1:size(X,2).&nbsp;If&nbsp;the&nbsp;Gaussians&nbsp;are</span><br>
<span class=help>%&nbsp;&nbsp;uni-variate&nbsp;then&nbsp;the&nbsp;covariaves&nbsp;can&nbsp;be&nbsp;given&nbsp;as&nbsp;a&nbsp;vector</span><br>
<span class=help>%&nbsp;&nbsp;Cov&nbsp;=&nbsp;[Cov_1,&nbsp;Cov_2,&nbsp;...,&nbsp;Cov_comp].</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;=&nbsp;pdfgauss(&nbsp;X,&nbsp;model&nbsp;)&nbsp;takes&nbsp;Gaussian&nbsp;parameters&nbsp;from&nbsp;structure</span><br>
<span class=help>%&nbsp;&nbsp;fields&nbsp;model.Mean&nbsp;and&nbsp;model.Cov.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Input&nbsp;matrix&nbsp;of&nbsp;column&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;Mean&nbsp;[dim&nbsp;x&nbsp;ncomp]&nbsp;Means&nbsp;of&nbsp;Gaussians.</span><br>
<span class=help>%&nbsp;&nbsp;Cov&nbsp;[dim&nbsp;x&nbsp;dim&nbsp;x&nbsp;ncomp]&nbsp;Covarince&nbsp;matrices.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;[ncomp&nbsp;x&nbsp;num_data]&nbsp;Values&nbsp;of&nbsp;probability&nbsp;density&nbsp;function.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;Univariate&nbsp;case</span><br>
<span class=help>%&nbsp;&nbsp;x&nbsp;=&nbsp;linspace(-5,5,100);</span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;=&nbsp;pdfgauss(x,0,1);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;plot(x,y)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;Multivariate&nbsp;case</span><br>
<span class=help>%&nbsp;&nbsp;[Ax,Ay]&nbsp;=&nbsp;meshgrid(linspace(-5,5,100),&nbsp;linspace(-5,5,100));</span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;=&nbsp;pdfgauss([Ax(:)';Ay(:)'],[0;0],[1&nbsp;0.5;&nbsp;0.5&nbsp;1]);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;surf(&nbsp;Ax,&nbsp;Ay,&nbsp;reshape(y,100,100));&nbsp;shading&nbsp;interp;</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;GSAMP,&nbsp;PDFGMM.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;28-apr-2004,&nbsp;VF</span><br>
<br>
<hr>
<span class=comment>%&nbsp;process&nbsp;input&nbsp;arguments</span><br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;3,<br>
&nbsp;&nbsp;arg1&nbsp;=&nbsp;c2s(arg1);<br>
&nbsp;&nbsp;Mean&nbsp;=&nbsp;arg1.Mean;<br>
&nbsp;&nbsp;Cov&nbsp;=&nbsp;&nbsp;arg1.Cov;<br>
<span class=keyword>else</span><br>
&nbsp;&nbsp;Mean&nbsp;=&nbsp;arg1;<br>
&nbsp;&nbsp;Cov&nbsp;=&nbsp;&nbsp;arg2;<br>
<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;get&nbsp;dimensions</span><br>
[dim,num_data]&nbsp;=&nbsp;size(X);<br>
ncomp&nbsp;=&nbsp;size(Mean,2);<br>
<br>
<span class=comment>%&nbsp;univariate&nbsp;variances&nbsp;can&nbsp;be&nbsp;given&nbsp;as&nbsp;a&nbsp;vector</span><br>
<span class=keyword>if</span>&nbsp;size(Cov,1)&nbsp;~=&nbsp;size(Cov,2),&nbsp;Cov&nbsp;=&nbsp;reshape(Cov,1,1,ncomp);&nbsp;<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;alloc&nbsp;memory</span><br>
y&nbsp;=&nbsp;zeros(ncomp,num_data);<br>
<br>
<span class=comment>%&nbsp;evaluate&nbsp;pdf&nbsp;for&nbsp;each&nbsp;component</span><br>
<span class=keyword>for</span>&nbsp;i=1:ncomp,<br>
&nbsp;&nbsp;dist&nbsp;=&nbsp;mahalan(X,Mean(:,i),Cov(:,:,i));<br>
&nbsp;&nbsp;y(i,:)&nbsp;=&nbsp;exp(-0.5*dist)/sqrt((2*pi)^dim*det(Cov(:,:,i)));<br>
<span class=keyword>end</span><br>
<br>
<span class=jump>return</span>;<br>
</code>
